<template>
  <page-layout>
    <a-row :gutter="[10, 10]">
      <!-- 中心区域 -->
      <a-col :span="24">
        <a-card>
          <div class="mb-20">
            <!-- 查询参数 -->
            <pro-query
              :searchParam="searchParam"
              @on-search ="search"
            ></pro-query>
          </div>
          <!-- 列表 -->
          <pro-table
            ref="tableRef"
            :fetch="fetch"
            :columns="columns"
            :operate="operate"
            :param="state.param"
            :pagination="pagination"
          >
          </pro-table>
        </a-card>
      </a-col>
    </a-row>

    <save :visible="state.visibleSave" @close="closeSave"></save>
    <info :visible="state.visibleInfo" @close="closeInfo"></info>
  </page-layout>
</template>
<script>
import save from './modal/save';
import info from './modal/info'
import { message , modal} from 'ant-design-vue';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { tree, remove, removeBatch } from "@/api/module/dept";
import { reactive, createVNode, ref } from 'vue';
const removeKey = "remove";
export default {
  components: {
    save,
    info
  },
  setup() {
    const tableRef = ref()
    const switchFormat = { yes: true, no: false, event: function(value,record){
        record.enable = !record.enable;
        return value;
    }}
    const columns = [
      { dataIndex: "name", key: "name", title: "合同编号" },
      { dataIndex: "address", key: "address", title: "合同甲方" },
      { dataIndex: "sort", key: "sort", title: "合同类型" },
      { dataIndex: "remark", key: "remark", title: "创建时间" }
    ];
    /// 数据来源 [模拟]
    const fetch = async (param) => {
      var response = await tree(param);
      console.log(response)
      // return {
      //   data: response.data,
      // };
    };
    /// 删除配置
    const removeMethod = (record) => {
      modal.confirm({
        title: '您是否确定要删除此配置?',
        icon: createVNode(ExclamationCircleOutlined),
        okText: '确定',
        cancelText: '取消',
        onOk() {
          message.loading({ content: "提交中...", key: removeKey });
          remove({"id":record.id}).then((response) => {
            if(response.success){
              message.success({content: "删除成功", key: removeKey, duration: 1}).then(()=>{
                tableRef.value.reload()
              })
            }else{
              message.error({content: "删除失败", key: removeKey, duration: 1})
            }
          })
        }
      });
    }
    /// 行操作
    const operate = [
      { label: "详情", event: function (record) { state.visibleInfo = true, state.recordInfo = record }},
      { label: "更改负责人", event: function (record) { state.visibleEdit = true, state.recordEdit = record }},
      // { label: "删除", event: function (record) { removeMethod(record) }},
    ];
    const openDialog = (type) => {
      state[type] = true
    }
    const pagination = false;
    const state = reactive({
      selectedRowKeys: [],
      param: {},
      visibleSave: false,
      visibleEdit: false,
      visibleInfo: false,
      recordEdit: {},
      recordInfo: {},
    })
    const searchParam = [
      { key: "laiyuan", type: "select", label: "合同类型", placeholder: '全部',
          options: [
          { text: "定金协议", value: 1},
          { text: "尾款协议", value: 2},
          { text: "课程合同", value: 3}
        ]
      },
      { key: "name", type: "input", label: "合同甲方", placeholder: '请输入'},
      { key: "school", type: "input", label: "合同编号", placeholder: '请输入'},
      { dataIndex:"createTime", key: "createTime", type: "rangeTime", label: "创建时间"}
    ]
    const search = function(value) {
      state.param = value
      tableRef.value.reload()
    }
    const onSelectChange = selectedRowKeys => {
      state.selectedRowKeys = selectedRowKeys;
    };
    const closeSave = function() {
      state.visibleSave = false
      tableRef.value.reload()
    }
    const closeEdit = function() {
      state.visibleEdit = false
      tableRef.value.reload()
    }
    const closeInfo = function() {
      state.visibleInfo = false
    }
    return {
      state,
      fetch,
      search,
      columns,
      operate,
      pagination,
      searchParam,
      onSelectChange,
      closeSave,
      closeEdit,
      closeInfo,
      openDialog,
      tableRef
    };
  },
};
</script>
